package com.cx330;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.junit.Test;

import java.io.*;

public class Example74 {
    @Test
    public void test1() {
        Student student = new Student("Cx330", 100);
        try (
                FileOutputStream fos = new FileOutputStream("student.obj");
                ObjectOutputStream oos = new ObjectOutputStream(fos)
        ) {
            oos.writeObject(student);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test2() {
        try (
                FileInputStream fis = new FileInputStream("student.obj");
                ObjectInputStream ois = new ObjectInputStream(fis)
        ) {
            Object stu = ois.readObject();
            System.out.println(stu);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

@Data
@AllArgsConstructor
@NoArgsConstructor
class Student implements Serializable {
    private String name;
    private int score;
}